package cn.edu.hbsi.service.impl;

import cn.edu.hbsi.domain.dto.UserLoginDTO;
import cn.edu.hbsi.domain.dto.UserRegistDTO;
import cn.edu.hbsi.domain.result.Result;
import cn.edu.hbsi.domain.result.ResultCodeEnum;
import cn.edu.hbsi.domain.vo.UserLoginVO;
import cn.edu.hbsi.utils.JwtHelper;
import cn.edu.hbsi.utils.MD5Util;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.hbsi.domain.entity.User;
import cn.edu.hbsi.service.UserService;
import cn.edu.hbsi.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author 21864
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2025-06-07 18:14:59
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public Result login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        String userPwd = MD5Util.encrypt(userLoginDTO.getUserPwd());
        if (!userPwd.equals(user.getUserPwd())){
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        String token = jwtHelper.createToken(user.getUid().longValue());
        Map data = new HashMap();
        data.put("token", token);
        return Result.ok(data);
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @Override
    public Result getUserInfo(String token) {
        if (jwtHelper.isExpiration(token)){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }

        Long userId = jwtHelper.getUserId(token);
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .uid(user.getUid())
                .username(user.getUsername())
                .userPwd(null)
                .nickName(user.getNickName())
                .build();
        Map data = new HashMap();
        data.put("userLogin", userLoginVO);
        return Result.ok(data);
    }

    /**
     * 检查用户名是否可用
     * @param username
     * @return
     */
    @Override
    public Result checkUserName(String username) {
        User user = userMapper.selectByUsername(username);
        if (user != null) {
            return Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        return Result.ok();
    }

    /**
     * 用户注册
     * @param userRegistDTO
     * @return
     */
    @Override
    public Result regist(UserRegistDTO userRegistDTO) {
        if (userMapper.selectByUsername(userRegistDTO.getUsername()) != null) {
            return Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        User user = User.builder()
                .username(userRegistDTO.getUsername())
                .userPwd(MD5Util.encrypt(userRegistDTO.getUserPwd()))
                .nickName(userRegistDTO.getNickName())
                .version(1)
                .isDeleted(0)
                .build();
        userMapper.insert(user);
        return Result.ok();
    }
}